/* eslint-disable eqeqeq *///忽略==的警告
import { getDeviceName, getDeviceDetailName } from "./stringUtils";
import { getDevicesDetailInfo } from "./scheduleUtils";
import { transIntToHex4Str } from "./dataUtils"
import { saveLightInfor, saveStateAreaSetInfor, saveAreaInfor, saveGroupSetInfor } from './homeUtils'
import PubSub from 'pubsub-js'
import { meshLog, SPRemove, SPGet, SPSet } from "../config";

export const getDeviceNumInfo = (devices) => {
    // 0 Area • 0 Light
    let numStr = ''
    const arr = devices.replace('[', '').replace(']', '').split(',');   //字符串转数组
    let count = arr.length
    // meshLog("wjk arr:",arr,"count:",count);
    if (devices === '[]') {
        count = 0
    }



    //只计算device
    if (count > 1) {
        numStr = count + ' Devices '
    } else {
        numStr = '0 Device '
    }
    return numStr;
}
export const getDeviceDescInfo = (arrInfo, isSimple) => {
    //计算area与device
    let numStr = ''
    let ma = 0;
    let md = 0;
    for (let m = 0; m < arrInfo.length; m++) {
        if (arrInfo[m].type == 1) {
            ma++;
        } else {
            md++;
        }
    }
    if (isSimple) {
        if (ma > 0 && md == 0) {
            numStr = ma + ' Area(s)'
        } else if (ma == 0 && md > 0) {
            numStr = md + ' Light(s)'
        } else {
            numStr = ma + ' Area(s) • ' + md + ' Light(s)'
        }
        return numStr;
    }
    if (ma == 0 && md == 0) {
        numStr = '0 Area • 0 Light'
    } else if (ma > 0 && md == 0) {
        numStr = ma + ' Areas'
        if (ma == 1) {
            numStr = '1 Area'
        }
    } else if (ma == 0 && md > 0) {
        numStr = md + ' Lights'
        if (md == 1) {
            numStr = '1 Light'
        }
    } else if (ma == 1 && md == 1) {
        numStr = '1 Area • 1 Light'
    } else if (ma > 1 && md == 1) {
        numStr = ma + ' Areas • 1 Light'
    } else if (ma == 1 && md > 1) {
        numStr = '1 Area • ' + md + ' Lights'
    } else {
        numStr = ma + ' Areas • ' + md + ' Lights'
    }
    return numStr;
}
// 计算  *Area + *subgroup + *Device
export const getAreaSubgroupDeviceInfo = (title, isIndivi, devices) => {
    if (devices === '[]') {
        return [];
    }
    const arr = devices.replace('[', '').replace(']', '').split(',');   //字符串转数组
    let arrayF = [];
    let countF = 0;
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    let groupSetx = JSON.parse(SPGet("groupSetx"));
    //添加area
    for (let m = 0; m < areaSetx.length; m++) {
        if (isIndivi) {  //独立模式
            break;
        }
        let countA = 0;
        for (let n = 0; n < deviceSetx.length; n++) {
            //过滤nano、遥控器
            if (deviceSetx[n].pid === '100E' || deviceSetx[n].pid === '100C') {
                continue;
            }
            if (areaSetx[m].areaNo === deviceSetx[n].areaNo) {
                countA++;
            }
        }
        let countD = 0;
        for (let n = 0; n < arr.length; n++) {
            // meshLog("wjk arrayF arr:",arr[n]);
            if (arr[n] < parseInt('C010', 16)) {
                for (let k = 0; k < deviceSetx.length; k++) {
                    if (arr[n] == parseInt(deviceSetx[k].address, 16) && deviceSetx[k].areaNo === areaSetx[m].areaNo) {
                        countD++;
                        break;
                    }
                }
            } else {
                for (let i = 0; i < groupSetx.length; i++) {
                    if (arr[n] == parseInt(groupSetx[i].groupNo, 16) && groupSetx[i].areaNo === areaSetx[m].areaNo) {
                        for (let k = 0; k < deviceSetx.length; k++) {
                            if (deviceSetx[k].groupNo === groupSetx[i].groupNo) {
                                countD++;
                            }
                        }
                    }
                }
            }
        }
        // meshLog("wjk arrayF countA:",countA);
        // meshLog("wjk arrayF countD:",countD);
        if (countA > 0 && countA == countD) {
            arrayF[countF] = { title: title, type: 1, areaNo: areaSetx[m].areaNo, name: areaSetx[m].name, pid: '', address: '', mac: '' };
            countF++;
        }
    }
    //添加subgroup
    for (let n = 0; n < arr.length; n++) {
        if (arr[n] > parseInt('C010', 16)) {
            let isAdd = true;
            for (let i = 0; i < arrayF.length; i++) {
                // meshLog("wjk arrayF areaNo:",arrayF[i].areaNo.substring(0, 3));
                // meshLog("wjk arrayF toString:",arr[n]);
                let m = arr[n] - parseInt(arrayF[i].areaNo, 16);
                // meshLog("wjk arrayF m:",m);
                if (m > 0 && m < 16) {
                    isAdd = false;
                }
            }
            if (isAdd) {
                let name = '';
                for (let i = 0; i < groupSetx.length; i++) {
                    if (arr[n] == parseInt(groupSetx[i].groupNo, 16)) {
                        arrayF[countF] = { title: title, type: 2, name: groupSetx[i].name, pid: '', address: '', mac: '' };
                        countF++;
                        break;
                    }
                }
            }
        }
    }
    //添加device
    for (let n = 0; n < arr.length; n++) {
        if (arr[n] < parseInt('C010', 16)) {
            let isAdd = true;
            for (let m = 0; m < deviceSetx.length; m++) {
                if (arr[n] == parseInt(deviceSetx[m].address, 16)) {
                    for (let i = 0; i < arrayF.length; i++) {
                        if (deviceSetx[m].areaNo === arrayF[i].areaNo) {
                            isAdd = false;
                            break
                        }
                    }
                    if (isAdd) {
                        arrayF[countF] = { title: title, type: 3, name: '', pid: deviceSetx[m].pid, address: deviceSetx[m].address, mac: deviceSetx[m].mac };
                        countF++;
                        break
                    }
                }
            }
        }
    }
    meshLog("wjk arrayF:", arrayF);
    return arrayF;
}
export const getDialogTitleInfo = (type, attrInfo) => {
    let numStr = ''
    numStr = type + ' : ' + getDeviceDescInfo(attrInfo, true);
    return numStr;
}
export const getDeviceListInfo = (devices) => {
    let infoAddr = [];
    const arr = devices.replace('[', '').replace(']', '').split(',');   //字符串转数组
    if (devices === '[]') {
        return infoAddr;
    }
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    meshLog("wjk deviceSetx:", deviceSetx);
    for (let i = 0; i < arr.length; i++) {
        // meshLog("wjk for arr:",parseInt(arr[i]));
        deviceSetx.filter((item) => {
            // meshLog("wjk deviceSet item:",parseInt(item.address,16));
            if (parseInt(arr[i]) == parseInt(item.address, 16)) {
                meshLog("wjk item.mac:", item.mac);
                infoAddr[i] = getDeviceName(parseInt(arr[i]), item.pid, item.mac)
            }
        })
    }
    meshLog("wjk infoAddr:", infoAddr);
    return infoAddr;
}
export const getDeviceArrInfo = (arrInfo) => {
    meshLog("wjk getDeviceArrInfo:", arrInfo);
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    let infoAddr = [];
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    for (let m = 0; m < arrInfo.length; m++) {
        if (arrInfo[m].type == 1 || arrInfo[m].type == 2) {
            infoAddr[m] = arrInfo[m].name;
        } else {
            deviceSetx.filter((item) => {
                if (arrInfo[m].address === item.address) {
                    let areaName = '';
                    for (let i = 0; i < areaSetx.length; i++) {
                        if (item.areaNo === areaSetx[i].areaNo) {
                            areaName = '_' + areaSetx[i].name;
                        }
                    }
                    infoAddr[m] = getDeviceDetailName(parseInt(item.address), item.pid, item.mac) + areaName;
                }
            })
        }
    }
    meshLog("wjk infoAddr:", infoAddr);
    return infoAddr;
}
//获取灯集合的areas（16进制）
export const getDeviceOccupyAreas = (devices) => {
    meshLog("wjk cur devices:", devices);
    let arrayA = [];
    let countA = 0;
    let mDevices = getDevicesDetailInfo(JSON.parse(devices));
    if (mDevices.length == 0) {
        return arrayA;
    }
    // meshLog("wjk mDevices:",mDevices);
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    for (let i = 0; i < mDevices.length; i++) {
        let str = mDevices[i].substring(2, 4) + mDevices[i].substring(0, 2);
        let areaNo = ''
        for (let n = 0; n < deviceSetx.length; n++) {
            if (parseInt(str, 16) == parseInt(deviceSetx[n].address, 16)) {
                areaNo = deviceSetx[n].areaNo;
                if (parseInt(deviceSetx[n].areaNo) == 0) {
                    areaNo = 'C000'
                }
            }
        }
        // meshLog("wjk ----areaNo:",areaNo);
        let isAdd = true;
        for (let i = 0; i < arrayA.length; i++) {
            if (arrayA[i] == areaNo) {
                isAdd = false;
            }
        }
        if (isAdd) {
            arrayA[countA] = areaNo;
            countA++;
        }
    }
    meshLog("wjk arrayA:", arrayA);
    return arrayA;
}
export const syncDataByScene = (sceneID) => {
    let scenesSet = JSON.parse(SPGet("scenesSetx"));
    meshLog("wjk sync scenesSet:", scenesSet);
    let count = 0;
    let lists = [];
    for (let m = 0; m < scenesSet.length; m++) {
        if (scenesSet[m].sceneID == sceneID) {
            if (scenesSet[m].isIndividual) {
                meshLog("wjk sync 0 details:", scenesSet[m].details);
                for (let j = 0; j < scenesSet[m].details.length; j++) {
                    let addr = scenesSet[m].details[j].address;
                    let model = scenesSet[m].details[j].colorStatus;
                    meshLog("wjk sync 0 addr:", addr);
                    setListData(lists, count, addr, model);
                    count++;
                }
            } else {
                meshLog("wjk sync 1 devices:", scenesSet[m].devices);
                if (scenesSet[m].devices == '[]') {
                    return;
                }
                const arr = scenesSet[m].devices.replace('[', '').replace(']', '').split(',');   //字符串转数组
                for (let j = 0; j < arr.length; j++) {
                    let addr = parseInt(arr[j]);
                    let model = scenesSet[m].colorStatus;
                    meshLog("wjk sync 1 addr:", addr);
                    setListData(lists, count, addr, model);
                    count++;
                }
            }
            break;
        }
    }
    meshLog("wjk syncDataByScene:", lists);
    //addr为单灯地址
    //[{addr: 2, isRGB: false, isOn: false, dim: 40, tem: 3500, hu: 0.288888,sa: 1,light: 0.460000}]
    for (let i = 0; i < lists.length; i++) {
        let addr = transIntToHex4Str(lists[i].addr)
        let isOnOff = '00'
        let dimx = Math.round(lists[i].dim / 100 * 65535)
        meshLog('addr', addr);
        if (lists[i].isOn == false) {
            isOnOff = '00'
            let devSetItem = ''
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            if (addr < 'C000') {
                //修改localStorage上灯的数据
                devSetItem = deviceSetx.filter((item) => {
                    if (item.address == addr) {
                        item.isOnoff = isOnOff
                        return item
                    }
                })
                meshLog('devSetItem', devSetItem);
                //saveLightInfor(devSetItem)
            } else {
                devSetItem = groupSetx.filter((item) => {
                    if (item.groupNo == addr) {
                        item.isOnoff = isOnOff
                        return item
                    }
                })
                meshLog('devSetItem', devSetItem);
                //saveGroupSetInfor(devSetItem)
            }
            let areaa = devSetItem[0].areaNo
            //修改localStorage上灯的数据结束
            //修改state中的areaSet开关的数据
            let devicearea = [];//属于这个组的全部灯和subgroup灯的集合
            let areaSetxxx = [];
            devicearea = []
            //meshLog("所在的areaNo所有灯的信息包括subgroup", devicearea);
            let deviceareaisOnoff = devicearea.map((item, key) => {
                return (
                    item.isOnoff = item.isOnoff
                )
            })
            //meshLog("deviceareaisOnoff", deviceareaisOnoff);
            let a = deviceareaisOnoff.find(item => item == '01');
            if (a == '01') {
                var aaa = '01'
            } else {
                var aaa = '00'
            }
            meshLog('aaa', areaa);
            const areaSetxx = JSON.parse(SPGet("areaSetx"));
            areaSetxxx = saveStateAreaSetInfor(areaSetxx, aaa, areaa);
            meshLog('areaSetxx', areaSetxxx);
            SPRemove('areaSetx');//删除loaclstorage上的deviceSetx
            SPSet('areaSetx', JSON.stringify(areaSetxxx))//从新上传到loaclstorage
            //meshLog('areaSetxxx', areaSetxxx);
            //修改组中灯的开关灯的反馈给下面调光调色的控制
            //PubSub.publish('lightdev', devSetItem)
        } else {
            isOnOff = '01'
            if (lists[i].isRGB == false) {
                //修改localStorage上deviceSetx的数据
                let devSetItem = ''
                let deviceSetx = JSON.parse(SPGet("deviceSetx"));
                let groupSetx = JSON.parse(SPGet("groupSetx"));
                //查询属于这个areaNo的 device
                if (addr < 'C000') {
                    //修改localStorage上灯的数据
                    devSetItem = deviceSetx.filter((item) => {
                        if (item.address == addr) {
                            item.isOnoff = isOnOff
                            return item
                        }
                    })
                    meshLog('devSetItem', devSetItem);
                    //saveLightInfor(devSetItem)
                } else {
                    devSetItem = groupSetx.filter((item) => {
                        if (item.groupNo == addr) {
                            item.isOnoff = isOnOff
                            return item
                        }
                    })
                    meshLog('devSetItem', devSetItem);
                    //saveGroupSetInfor(devSetItem)
                }
                let areaa = devSetItem[0].areaNo
                //修改state中的areaSet开关的数据
                let devicearea = [];//属于这个组的全部灯和subgroup灯的集合
                let areaSetxxx = [];
                devicearea = []
                //meshLog("所在的areaNo所有灯的信息包括subgroup", devicearea);
                let deviceareaisOnoff = devicearea.map((item, key) => {
                    return (
                        item.isOnoff = item.isOnoff
                    )
                })
                //meshLog("deviceareaisOnoff", deviceareaisOnoff);
                let a = deviceareaisOnoff.find(item => item == '01');
                if (a == '01') {
                    var aaa = '01'
                } else {
                    var aaa = '00'
                }
                meshLog('aaa', areaa);
                const areaSetxx = JSON.parse(SPGet("areaSetx"));
                areaSetxxx = saveStateAreaSetInfor(areaSetxx, aaa, areaa);
                meshLog('areaSetxx', areaSetxxx);
                SPRemove('areaSetx');//删除loaclstorage上的deviceSetx
                SPSet('areaSetx', JSON.stringify(areaSetxxx))//从新上传到loaclstorage
                // setTimeout(() => {
                //     PubSub.publish('RGBrgbHneifanhui', 1)
                //     PubSub.publish('temneifanhui', 1)
                // }, 500)
            } else {
                let devSetItem = ''
                let deviceSetx = JSON.parse(SPGet("deviceSetx"));
                let groupSetx = JSON.parse(SPGet("groupSetx"));
                if (addr < 'C000') {
                    //修改localStorage上灯的数据
                    devSetItem = deviceSetx.filter((item) => {
                        if (item.address == addr) {
                            item.isOnoff = isOnOff
                            return item
                        }
                    })
                    meshLog('devSetItem', devSetItem);
                    //saveLightInfor(devSetItem)
                } else {
                    devSetItem = groupSetx.filter((item) => {
                        if (item.groupNo == addr) {
                            item.isOnoff = isOnOff
                            return item
                        }
                    })
                    meshLog('devSetItem', devSetItem);
                    //saveGroupSetInfor(devSetItem)
                }
                let areaSetx = JSON.parse(SPGet("areaSetx"));
                let areaitem;
                for (let index = 0; index < areaSetx.length; index++) {
                    if (areaSetx[index].areaNo == devSetItem[0].areaNo) {
                        areaitem = areaSetx[index]
                    }
                }
                //meshLog('areaitem', areaitem);
                areaitem.rgbH = lists[i].hu
                // saveAreaInfor(areaitem)
                let areaa = devSetItem[0].areaNo
                //修改state中的areaSet开关的数据
                let devicearea = [];//属于这个组的全部灯和subgroup灯的集合
                let areaSetxxx = [];
                devicearea = []
                //meshLog("所在的areaNo所有灯的信息包括subgroup", devicearea);
                let deviceareaisOnoff = devicearea.map((item, key) => {
                    return (
                        item.isOnoff = item.isOnoff
                    )
                })
                //meshLog("deviceareaisOnoff", deviceareaisOnoff);
                let a = deviceareaisOnoff.find(item => item == '01');
                if (a == '01') {
                    var aaa = '01'
                } else {
                    var aaa = '00'
                }
                meshLog('aaa', areaa);
                const areaSetxx = JSON.parse(SPGet("areaSetx"));
                areaSetxxx = saveStateAreaSetInfor(areaSetxx, aaa, areaa);
                meshLog('areaSetxx', areaSetxxx);
                SPRemove('areaSetx');//删除loaclstorage上的deviceSetx
                SPSet('areaSetx', JSON.stringify(areaSetxxx))//从新上传到loaclstorage
                //meshLog('areaSetxxx', areaSetxxx);
                //修改组中灯的开关灯的反馈给下面调光调色的控制
                // PubSub.publish('lightdev', devSetItem)
                // setTimeout(() => {
                //     PubSub.publish('rgbHneifanhui', lists[i].hu)
                // }, 500)
                // //修改亮度的背景条颜色
                // PubSub.publish('LightbackgroudColol', devSetItem)
                // PubSub.publish('RGBrgbHneifanhui', devSetItem)//返回给area组内
            }
        }
        meshLog('lists[i]', addr);
    }
}
function setListData(lists, count, addr, model) {
    lists[count] = {
        addr: addr, isRGB: model.adjustType == 1 ? true : false, isOn: model.isOnOff,
        dim: model.dim, tem: model.temperature,
        hu: model.hue, sa: model.saturation, light: model.brightness
    };
}
